/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuarios;

import dataBase.Db;
import hate.EntradaSalida;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author martinLequerica
 */
public class Editar extends JFrame{
    
    
private String[] etiquetas={"Nombre","Apellido","Telefono","Direccion","Nombre Usuario","Contrasenia","identificador"};
    private JTextField[] text;
    private JButton aceptar,cancelar;
    
    public Editar(final Vendedor v){
        super("Editar Usuario");
        text = new JTextField[7];
        setLayout(new GridLayout(etiquetas.length+1, 2));
        aceptar = new JButton("Aceptar");
        cancelar = new JButton("Cancelar");
        
        
        for (int i =0 ;i<etiquetas.length; i++){
            
            add(new JLabel(etiquetas[i]));
            text[i] = new JTextField();
            add(text[i]);
            
            
            
        }
        
        add(aceptar);
        add(cancelar);
        
        
        cargarCampos(v);
        
        aceptar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                String passwd="";
                Db db = new Db();
                Statement st = db.getStatement();
                
                if (text[5].getText().equals("")){
                    String sql = "update vendedor set nombre = '"+text[0].getText()+"', apellido = '"+text[1].getText()+"', telefono = "+text[2].getText()+",direccion = '"+text[3].getText()+"' where idvendedor = "+v.getIdVendedor()+"  ";
                    try {
                        st.execute(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(Editar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }else {
                    JOptionPane.showMessageDialog(null, text[5].getText());
                 passwd = new EntradaSalida().hash(text[5].getText());
                    String sql = "update vendedor set nombre = '"+text[0].getText()+"', apellido = '"+text[1].getText()+"', telefono = "+text[2].getText()+",direccion = '"+text[3].getText()+"',contrasenia = '"+passwd+"'  where vendedor.idvendedor= "+v.getIdVendedor()+" ";
                    try {
                        System.out.println(sql);
                        st = db.getStatement();
                        st.execute(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(Editar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        st.execute(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(Editar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
                
                
                
                
                
            }
        });
        
        
        
        cancelar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
           
                setVisible(false);
                
            }
        });
               
        setLocation(400,400);
        setSize(260,260);
        setVisible(true);
        
    }

    private void cargarCampos(Vendedor v) {
       
        
        text[0].setText(v.getNombre());
        text[1].setText(v.getApellido());
        text[2].setText(String.valueOf(v.getTelefono()));
        text[3].setText(v.getDireccion());
        text[4].setText(v.getNombreUsuario());
        text[4].setEditable(false);
        
        text[6].setText(v.getIdentificador());
        text[6].setEditable(false);
        
        
    }
    
}
